package com.dhl.fin.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.domain.BankStatement;
import com.dhl.fin.api.domain.BusinessRecord;
import com.dhl.fin.api.dto.BankStatementRequestDTO;
import com.dhl.fin.api.service.BankStatementService;
import com.dhl.fin.api.service.BusinessRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("bankstatement")
public class BankStatementController extends CommonController<BankStatement> {

    @Autowired
    private BankStatementService bankStatementService;
    @Autowired
    private BusinessRecordService businessRecordService;

    @ResponseBody
    @PostMapping("incomemoney")
    public ApiResponse incomeMoney(@RequestBody BankStatementRequestDTO bankStatementRequestDTO) throws Exception {

        //保存一条流水记录数据
        BankStatement bankStatement = bankStatementService.incomeBankStatement(bankStatementRequestDTO);

        //保存一条业务记录数据
        businessRecordService.addBusinessRecord(bankStatement);

        return ApiResponse.success();
    }

    @ResponseBody
    @PostMapping("outmoney")
    public ApiResponse outmoney(@RequestBody BankStatementRequestDTO bankStatementRequestDTO) throws Exception {
        bankStatementService.outMoneyBankStatement(bankStatementRequestDTO);
        return ApiResponse.success();
    }

    @ResponseBody
    @GetMapping("calculateInterest")
    public ApiResponse calculateInterest(Double money, String unicode) throws Exception {
        List<Map> bankStatement = bankStatementService.calculateInterest(money, unicode);
        return ApiResponse.success(bankStatement);
    }


    @ResponseBody
    @PostMapping("fixIncome")
    public ApiResponse fixIncome(@RequestBody BankStatementRequestDTO bankStatementRequestDTO) throws Exception {
        bankStatementService.fixIncomeBankStatement(bankStatementRequestDTO);
        return ApiResponse.success();
    }

    @ResponseBody
    @PostMapping("fixOut/{id}")
    public ApiResponse fixOut(@PathVariable Long id) throws Exception {
        bankStatementService.fixOut(id);
        return ApiResponse.success();
    }

    @ResponseBody
    @PostMapping("timeToFix")
    public ApiResponse timeToFix(@RequestBody BankStatementRequestDTO bankStatementRequestDTO) throws Exception {
        bankStatementService.timeToFix(bankStatementRequestDTO);
        return ApiResponse.success();
    }

    @ResponseBody
    @RequestMapping("totalSummary")
    public ApiResponse calculateTotalSummary(Long bankAccountId, Integer lastMonth) throws Exception {
        Map result = bankStatementService.calculateTotalSummary(bankAccountId, lastMonth);
        return ApiResponse.success(result);
    }

    @ResponseBody
    @RequestMapping("dataAnalysis/{bankAccountId}")
    public ApiResponse dataAnalysis(@PathVariable Long bankAccountId) throws Exception {
        Map result = bankStatementService.dataAnalysis(bankAccountId);
        return ApiResponse.success(result);
    }


}
